package com.future.bantang.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.bantang.R;
import com.future.bantang.app.BanTangAPP;
import com.future.bantang.entity.RecommendHotDetailEntity;
import com.future.bantang.view.LineWrapLayout;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by liufujie on 16-1-15.
 * 热门商品类适配器
 */
public class RecommendHotAdapter extends AbsBaseAdapter<RecommendHotDetailEntity.DataEntity.ListEntity> implements View.OnClickListener {

    private ImageLoader imageLoader;

    private Context context;

    private Callback callback;

    /**
     * 初始化
     *
     * @param context
     */
    public RecommendHotAdapter(Context context, Callback callback) {
        super(context, R.layout.item_recommend_hot_show_layout);
        imageLoader = BanTangAPP.getApp().getImageLoader();
        this.context = context;
        this.callback = callback;
    }

    /**
     * 绑定控件，并赋值给控件
     *
     * @param viewHolder
     * @param data
     */
    @Override
    public void bindDatas(ViewHolder viewHolder, RecommendHotDetailEntity.DataEntity.ListEntity data) {


        getViewsToAdapter(viewHolder);

        //头部控件设置
        imageLoader.displayImage(data.getAuthor().getAvatar(), ivUserPic);
        //通过接口回调让fragment实现点击方法
        ivUserPic.setOnClickListener(this);
        //把数据存放到控件中，下面所有情况相同
        ivUserPic.setTag(data.getAuthor_id());

        imageLoader.displayImage(data.getPics().get(0).getUrl(), ivRecommenedHotPic);

        tvUserName.setText(data.getAuthor().getUsername());
        tvUserName.setOnClickListener(this);
        tvUserName.setTag(data.getAuthor_id());

        tvRecommenedTime.setText(data.getDatestr());
        //用户分享
        ivUserShare.setOnClickListener(this);
        ivUserShare.setTag(data);

        //中间控件设置
        tvGoodsDesc.setText(data.getContent());

        tvRecommenedHotLike.setText(String.format(
                "%s人喜欢", data.getDynamic().getLikes()));

        tvRecommenedAllCommts.setText(String.format(
                "所有%s条评论", data.getDynamic().getComments()
        ));
        tvRecommenedAllCommts.setOnClickListener(this);
        tvRecommenedAllCommts.setTag(data.getComments());


        setRecommenedHotTags(data.getTags());

        setRecommenedHotCommts(data.getComments());

        if (data.getProduct().size() != 0) {
            setRecommenedHotShare(data.getProduct().get(0));
        }

        llRecommenedHotShare.setOnClickListener(this);
        llRecommenedHotShare.setTag(data.getShare_url());

        llRecommenedHotLike.setOnClickListener(this);
        /*TODO 重新抓包，数据有误*/
        llRecommenedHotLike.setTag(data.getDynamic().getLikes_users());

        llRecommenedHotCommts.setOnClickListener(this);
        llRecommenedHotCommts.setTag(data.getComments());

    }

    /**
     * 设置分享界面
     *
     * @param productEntity
     */
    private void setRecommenedHotShare(RecommendHotDetailEntity.DataEntity.ListEntity.ProductEntity productEntity) {
        imageLoader.displayImage(productEntity.getPic(), ivRecommenedSharePic);
        tvRecommenedHotName.setText(productEntity.getTitle());
        if (productEntity.getPlatform().equals("1")) {
            ivRecommenedHotFrom.setImageResource(R.mipmap.ic_goods_store_tb);
            tvRecommenedHotFrom.setText("淘宝");
        } else if (productEntity.getPlatform().equals("2")) {
            ivRecommenedHotFrom.setImageResource(R.mipmap.ic_goods_store_tm);
            tvRecommenedHotFrom.setText("天猫");
        } else {
            ivRecommenedHotFrom.setImageResource(R.mipmap.ic_goods_store_jd);
            tvRecommenedHotFrom.setText("京东");
        }
        tvRecommenedHotPrice.setText(String.format("￥%s", productEntity.getPrice()));
    }

    private View itemView;

    /**
     * 动态添加评论
     *
     * @param comments
     */
    private void setRecommenedHotCommts(List<RecommendHotDetailEntity.DataEntity.ListEntity.CommentsEntity> comments) {
        int index = comments.size() < 3 ? comments.size() : 3;
        LinearLayout.LayoutParams paramsFirst = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT
                , LinearLayout.LayoutParams.WRAP_CONTENT
        );
        paramsFirst.setMargins(0, 5, 0, 0);
        for (int i = 0; i < index; i++) {
            itemView = View.inflate(context, R.layout.recommend_hot_commts_layout, null);
            itemView.setId(i);
            itemView.setLayoutParams(paramsFirst);
            llRecommenedHotCommts.addView(itemView);

            TextView tv1 = (TextView) itemView.findViewById(R.id.tv_recommed_hot_username);
            tv1.setText(comments.get(i).getUsername());
            TextView tv2 = (TextView) itemView.findViewById(R.id.tv_recommed__hot_commt);
            tv2.setText(comments.get(i).getConent());
        }
    }

    /**
     * 动态添加tag标签
     *
     * @param tags
     */
    private void setRecommenedHotTags(List<RecommendHotDetailEntity.DataEntity.ListEntity.TagsEntity> tags) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT
                , LinearLayout.LayoutParams.WRAP_CONTENT
        );
        params.setMargins(40, 0, 0, 0);

        LinearLayout.LayoutParams paramsFirst = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT
                , LinearLayout.LayoutParams.WRAP_CONTENT
        );
        paramsFirst.setMargins(10, 0, 0, 0);


        for (int i = 0; i < tags.size(); i++) {
            TextView tvTag = new TextView(context);
            if (i == 0) {
                tvTag.setLayoutParams(paramsFirst);
            } else {
                tvTag.setLayoutParams(params);
            }
            tvTag.setTextColor(Color.RED);
            tvTag.setText(tags.get(i).getName());
            llRecommenedHotTag.addView(tvTag);
        }
    }

    //控件列表
    ImageView ivUserPic, ivRecommenedHotPic, ivUserTalk, ivUserLike, ivUserShare, ivUserMore;
    TextView tvUserName, tvRecommenedTime;

    LinearLayout llRecommenedHotLike, llRecommenedHotCommts, llRecommenedHotShare;
    TextView tvGoodsDesc, tvRecommenedHotLike, tvRecommenedAllCommts,
            tvRecommenedHotName, tvRecommenedHotFrom, tvRecommenedHotPrice;
    ImageView ivRecommenedSharePic, ivRecommenedHotFrom;
    LineWrapLayout llRecommenedHotTag;


    private void getViewsToAdapter(ViewHolder viewHolder) {

        //头部控件
        ivUserPic = (ImageView) viewHolder.getView(R.id.iv_user_pic);

        ivRecommenedHotPic = (ImageView) viewHolder.getView(R.id.iv_recommed_hot_pic);

        ivUserTalk = (ImageView) viewHolder.getView(R.id.iv_user_talk);

        ivUserLike = (ImageView) viewHolder.getView(R.id.iv_user_like);

        ivUserShare = (ImageView) viewHolder.getView(R.id.iv_user_share);

        ivUserMore = (ImageView) viewHolder.getView(R.id.iv_user_more);

        tvUserName = (TextView) viewHolder.getView(R.id.tv_user_name);

        tvRecommenedTime = (TextView) viewHolder.getView(R.id.tv_recommend_time);

        //中间部分控件
        llRecommenedHotTag = (LineWrapLayout) viewHolder.getView(R.id.ll_recommed_hot_tag);

        llRecommenedHotLike = (LinearLayout) viewHolder.getView(R.id.ll_recommed_hot_like);

        tvRecommenedHotLike = (TextView) viewHolder.getView(R.id.tv_recommed_hot_like);

        tvGoodsDesc = (TextView) viewHolder.getView(R.id.tv_goods_desc);

        tvRecommenedAllCommts = (TextView) viewHolder.getView(R.id.tv_recommed_allcommts);

        llRecommenedHotCommts = (LinearLayout) viewHolder.getView(R.id.ll_recommed_hot_commts);

        llRecommenedHotShare = (LinearLayout) viewHolder.getView(R.id.ll_recommed_hot_share);

        tvRecommenedHotName = (TextView) viewHolder.getView(R.id.tv_recommed__hot_name);

        tvRecommenedHotFrom = (TextView) viewHolder.getView(R.id.tv_recommed__hot_from);

        tvRecommenedHotPrice = (TextView) viewHolder.getView(R.id.tv_recommed__hot_price);

        ivRecommenedSharePic = (ImageView) viewHolder.getView(R.id.iv_recommed_hot_share_pic);

        ivRecommenedHotFrom = (ImageView) viewHolder.getView(R.id.iv_recommed_hot_from);

        //加载控件前 清除控件
        llRecommenedHotCommts.removeAllViews();
        llRecommenedHotTag.removeAllViews();
    }

    @Override
    public void onClick(View v) {
        callback.onclick(v);
    }

    public interface Callback {
        void onclick(View view);
    }

}
